package com.consumer.order.service;
import com.consumer.order.dao.orederGoodsDao;
import com.consumer.order.entity.orderGoods;
import com.goods.entity.*;
import com.consumer.order.dao.ordersDao;
import com.consumer.order.entity.orders;
import com.consumer.order.feign.goodsFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class orderServiceImpl implements orderService{

    @Autowired
    private ordersDao dao;

    @Autowired
    private orederGoodsDao goodsDao;
    @Autowired
    private goodsFeign feign;

    @Override
    public orders create(Integer[] gid) {
        goods g = null;
        Double allprice = 0.0;
        for(Integer i:gid)
        {
            g = feign.findOne(i);
            orderGoods orderGoods = new orderGoods();
            orderGoods.setGid(g.getGid());
            orderGoods.setSid(g.getSid());
            orderGoods.setNum(1);
            goodsDao.save(orderGoods);
            allprice = allprice + Double.parseDouble(g.getPrice());
        }
        orders o = new orders();
        o.setStatus("未发货");
        o.setAllprice(allprice.toString());
        return dao.save(o);
    }

    @Override
    public void delete(orders o) {
        dao.delete(o);
    }

    @Override
    public orders update(orders o) {
        return dao.save(o);
    }

    @Override
    public orders findOne(Integer oid) {
        return dao.findById(oid).orElse(null);
    }

    @Override
    public List<orders> findAll() {
        return dao.findAll();
    }

    public List<goods> findAllgoods(){return feign.findAll();}
}
